﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public float jumpForce = 10f;
    public Rigidbody2D rb;
    public SpriteRenderer sr;

    public string currentColor;

    public Color colorBlue;
    public Color colorYellow;
    public Color colorPink;
    public Color colorPurple;

    public GameManager gameManager;

    private void Start()
    {
        SetRandomColor();
    }

    private void Update()
    {
        if (!gameManager.isLose)
        {
            if (Input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
            {
                rb.velocity = Vector2.up * jumpForce;
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Trap")
        {
            gameManager.isLose = true;
        }

        if (collision.tag == "ColorChanger")
        {
            if (gameManager.isLose != true)
            {
                SetRandomColor();
                Destroy(collision.gameObject);
                gameManager.score++;
                return;
            }
        }

        if (collision.tag != currentColor)
        {
            gameManager.isLose = true;
        }
    }

    void SetRandomColor()
    {
        int index = Random.Range(0, 4);

        switch (index)
        {
            case 0:
                currentColor = "Blue";
                sr.color = colorBlue;
                break;
            case 1:
                currentColor = "Yellow";
                sr.color = colorYellow;
                break;
            case 2:
                currentColor = "Pink";
                sr.color = colorPink;
                break;
            case 3:
                currentColor = "Purple";
                sr.color = colorPurple;
                break;
        }
    }
}
